﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using App.Codes;
using Langben.DAL;
using PanGu;
using PanGu.HighLight;

namespace App.Controllers
{
    public class YeWuSerachController : Controller
    {
        //
        // GET: /YeWuSerach/

        public ActionResult Index()
        {
            return View();
        }
        public string GetFenCi(string source)
        {
            IQueryable<Business> businessesList = from s in new SysEntities().Business
                select s;
            StringBuilder sb=new StringBuilder();
            //创建HTMLFormatter,参数为高亮单词的前后缀
            PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            //创建Highlighter ，输入HTMLFormatter 和盘古分词对象Semgent
            PanGu.HighLight.Highlighter highlighter =new PanGu.HighLight.Highlighter(simpleHTMLFormatter,new Segment());
           //设置每个摘要段的字符数
            highlighter.FragmentSize = 4000;
            //获取最匹配的摘要段
            int count = 0;
            foreach (var businesses in businessesList)
            {
                if (string.IsNullOrEmpty(businesses.Name))
                {
                    businesses.Name = string.Empty;
                }
                if (string.IsNullOrEmpty(businesses.Describe))
                {
                    businesses.Describe = string.Empty;
                }
                if (string.IsNullOrEmpty(businesses.CaiLiao))
                {
                    businesses.CaiLiao = string.Empty;
                }

                if (!string.IsNullOrEmpty(highlighter.GetBestFragment(source, businesses.Name))
                    || !string.IsNullOrEmpty(highlighter.GetBestFragment(source, businesses.Describe))
                    || !string.IsNullOrEmpty(highlighter.GetBestFragment(source, businesses.CaiLiao)))
                {
                    count++;
                    string name = string.IsNullOrEmpty(highlighter.GetBestFragment(source, businesses.Name))
                        ? businesses.Name
                        : highlighter.GetBestFragment(source, businesses.Name);

                    string describe = string.IsNullOrEmpty(highlighter.GetBestFragment(source, businesses.Describe))
                        ? businesses.Describe
                        : highlighter.GetBestFragment(source, businesses.Describe);

                    string caiLiao = string.IsNullOrEmpty(highlighter.GetBestFragment(source, businesses.CaiLiao))
                        ? businesses.CaiLiao
                        : highlighter.GetBestFragment(source, businesses.CaiLiao);

                    sb.Append("<tr>");
                    sb.Append("<td class='text4' colspan='2'>");
                    sb.Append(name);
                    sb.Append("</td>");
                    sb.Append("</tr>");

                    sb.Append("<tr>");
                    sb.Append("<td class='text2'>办理地点:</td>");

                    sb.Append("<td class='td2'>");

                    string businessid = businesses.RowGuid;
                    string personid =
                        new ClsCon().GetFieldValue(
                            "SELECT TOP 1 PersonGuid FROM dbo.PersonAndBusiness WHERE BusinessGuid='" + businessid + "'");
                    if (!string.IsNullOrEmpty(personid))
                    {
                        SysPerson person = new SysEntities().SysPerson.SingleOrDefault(s => s.Id == personid);

                        if (person!=null && !string.IsNullOrEmpty(person.OfficeSpaceGuid))
                        {
                            OfficeSpace officeSpace =
                                new SysEntities().OfficeSpace.SingleOrDefault(s => s.RowGuid == person.OfficeSpaceGuid);
                            if (officeSpace != null)
                            {
                                sb.Append("请您前往"+officeSpace.Floor+","+officeSpace.Name+","+person.MyName+"处办理。");
                            }
                        }
                    }

                    
                    sb.Append("</td>");
                    sb.Append("</tr>");

                    sb.Append("<tr style='height:10px;'>");
                    sb.Append("<td > </td>");

                    sb.Append("<td ");
                  
                    sb.Append("</td>");
                    sb.Append("</tr>");


                    sb.Append("<tr>");
                    sb.Append("<td class='text2'>相关材料:</td>");

                    sb.Append("<td class='td2'>");
                    sb.Append(caiLiao);
                    sb.Append("</td>");
                    sb.Append("</tr>");
                }
                else
                {
                    if (businesses.Name.Contains(source) || businesses.Describe.Contains(source) || businesses.CaiLiao.Contains(source))
                    {
                        count++;
                        string name = businesses.Name.Contains(source)
                            ? businesses.Name.Replace(source, "<font color=\"red\">" + source + "</font>")
                            : businesses.Name;

                        string describe = businesses.Describe.Contains(source)
                            ? businesses.Describe.Replace(source, "<font color=\"red\">" + source + "</font>")
                            : businesses.Describe;

                        string caiLiao = businesses.CaiLiao.Contains(source)
                            ? businesses.CaiLiao.Replace(source, "<font color=\"red\">" + source + "</font>")
                            : businesses.CaiLiao;

                        sb.Append("<tr>");
                        sb.Append("<td class='text4' colspan='2'>");
                        sb.Append(name);
                        sb.Append("</td>");
                        sb.Append("</tr>");

                        sb.Append("<tr>");
                        sb.Append("<td class='text2'>办理地点:</td>");

                        sb.Append("<td class='td2'>");

                        string businessid = businesses.RowGuid;
                        string personid =
                            new ClsCon().GetFieldValue(
                                "SELECT TOP 1 PersonGuid FROM dbo.PersonAndBusiness WHERE BusinessGuid='" + businessid + "'");
                        if (!string.IsNullOrEmpty(personid))
                        {
                            SysPerson person = new SysEntities().SysPerson.SingleOrDefault(s => s.Id == personid);

                            if (person != null && !string.IsNullOrEmpty(person.OfficeSpaceGuid))
                            {
                                OfficeSpace officeSpace =
                                    new SysEntities().OfficeSpace.SingleOrDefault(s => s.RowGuid == person.OfficeSpaceGuid);
                                if (officeSpace != null)
                                {
                                    sb.Append("请您前往" + officeSpace.Floor + "," + officeSpace.No + officeSpace.Name + "," + person.MyName + "处办理。");
                                }
                            }
                        }


                        sb.Append("</td>");
                        sb.Append("</tr>");

                        sb.Append("<tr style='height:10px;'>");
                        sb.Append("<td > </td>");

                        sb.Append("<td ");

                        sb.Append("</td>");
                        sb.Append("</tr>");


                        sb.Append("<tr>");
                        sb.Append("<td class='text2'>相关材料:</td>");

                        sb.Append("<td class='td2'>");
                        sb.Append(caiLiao);
                        sb.Append("</td>");
                        sb.Append("</tr>");
                    }
                }

                
                
            }
            if (count > 2)
            {
                sb.Append("<tr><td><input id='ismarquee' value='" + 1 + "' style='display:none' /></td></tr>");
            }
            else
            {
                sb.Append("<tr><td><input id='ismarquee' value='" + 0 + "' style='display:none' /></td></tr>");
            }


            return sb.ToString();
        }
    }
}
